package cc.booku.owlbox.UserSettingsPage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.net.Uri;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.dueeeke.videocontroller.MarqueeTextView;
import com.sunfusheng.marqueeview.MarqueeView;
import com.timmy.tdialog.TDialog;
import com.timmy.tdialog.base.BindViewHolder;
import com.timmy.tdialog.listener.OnBindViewListener;
import com.timmy.tdialog.listener.OnViewClickListener;

import java.util.ArrayList;
import java.util.List;

import cc.booku.owlbox.ArtDesign.SystemUtil;
import cc.booku.owlbox.CollectionStation_norm.CS_FileScannerActivity;
import cc.booku.owlbox.FileOperations.FileoOperations;
import cc.booku.owlbox.InitPage.TextRoundActivity;
import cc.booku.owlbox.R;
import cc.booku.owlbox.VideoPlayer.FullScreen.FullScreenActivity;
import cc.booku.owlbox.loadSqlite.DatabaseHelper;

public class MyselfActivity extends AppCompatActivity {
    private Toolbar Toolbar;
    private MarqueeView tv_board;
    private TextView Tv_microblog, tv_videodownload, tv_history, tv_favorites, Tv_aboutapp, tv_search, Tv_update;
    private DatabaseHelper helper;
    private SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myself);
        tv_board = (MarqueeView) findViewById(R.id.tv_board);
        String BillBoard_V = FileoOperations.readAssetsTxt(MyselfActivity.this, "TxtData/BillBoard");
        JSONObject objsd = JSONObject.parseObject(BillBoard_V);
        String result1 = objsd.getString("Buser1");
        String result2 = objsd.getString("Buser2");
        List<String> messages = new ArrayList<>();
        messages.add(result1);
        messages.add(result2);
// 在代码里设置自己的动画
        tv_board.startWithList(messages, R.anim.anim_bottom_in, R.anim.anim_top_out);


        //加载标题栏
        LoadToolbar();
        //状态栏颜色
        SystemUtil.setStatusBarColor(this, Color.parseColor("#448aca"));
        initsqlite();

        tv_history = (TextView) findViewById(R.id.tv_history);
        tv_history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MyselfActivity.this, "历史记录", Toast.LENGTH_SHORT).show();


                //Toast.makeText(getContext(),"历史记录",Toast.LENGTH_SHORT).show();
                Cursor c = db.rawQuery("select * from collectTable", null);
                if (c.getCount() > 0) {
                    Intent intent = new Intent(MyselfActivity.this, UserDataActivity.class);
                    //用Bundle携带数据
                    Bundle bundle = new Bundle();
                    //传递name参数为tinyphp
                    bundle.putString("sqlSentence", "collectTable");
                    bundle.putString("sqlTitle", "历史纪录");
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(MyselfActivity.this, "您还没有观看记录", Toast.LENGTH_SHORT).show();

                }

            }
        });

        tv_favorites = (TextView) findViewById(R.id.tv_favorites);
        tv_favorites.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(getContext(),"我的收藏",Toast.LENGTH_SHORT).show();

                Cursor c = db.rawQuery("select * from loveTable", null);
                if (c.getCount() > 0) {
                    Intent intent = new Intent(MyselfActivity.this, UserDataActivity.class);
                    //用Bundle携带数据
                    Bundle bundle = new Bundle();
                    //传递name参数为tinyphp
                    bundle.putString("sqlSentence", "loveTable");
                    bundle.putString("sqlTitle", "我的收藏");
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(MyselfActivity.this, "您还没有收藏", Toast.LENGTH_SHORT).show();

                }
            }
        });

        tv_search = (TextView) findViewById(R.id.tv_search);
        tv_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MyselfActivity.this, "待开发", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MyselfActivity.this, loadSearchActivity.class);
                startActivity(intent);

            }
        });


        tv_videodownload = (TextView) findViewById(R.id.tv_videodownload);
        tv_videodownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Toast.makeText(MyselfActivity.this, "我的下载", Toast.LENGTH_SHORT).show();


                Cursor c = db.rawQuery("select * from downTable", null);
                if (c.getCount() > 0) {
                    Intent intent = new Intent(MyselfActivity.this, UserDataActivity.class);
                    //用Bundle携带数据
                    Bundle bundle = new Bundle();
                    //传递name参数为tinyphp
                    bundle.putString("sqlSentence", "downTable");
                    bundle.putString("sqlTitle", "下载内容");
                    intent.putExtras(bundle);
                    startActivity(intent);
                } else {
                    Toast.makeText(MyselfActivity.this, "您还没有下载视频", Toast.LENGTH_SHORT).show();

                }
            }
        });

        //关注新浪微博
        Tv_microblog = (TextView) findViewById(R.id.tv_microblog);
        Tv_microblog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                try {
//                    openUserInfoByUid(MyselfActivity.this, "7455703884");
//                } catch (Exception e) {
//                    //异常处理
//                    Tv_microblog.setText("打开失败！点击重试~");
//                }

                try {
                    SystemUtil.joinQQGroup(MyselfActivity.this, "WZXjv7I736JKIJ48YTlIfCuLvHlWTx0a");
                } catch (Exception e) {
                    //异常处理
                    Tv_microblog.setText("打开失败！点击重试~");
                }

            }
        });


        Tv_aboutapp = (TextView) findViewById(R.id.tv_aboutapp);
        Tv_aboutapp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击显示弹窗用于修改

                Dialogaboutsoft("当前软件版本：" + packageName(MyselfActivity.this));


            }
        });


        Tv_update = (TextView) findViewById(R.id.tv_update);
        Tv_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //点击显示弹窗用于修改
                try {

                    Intent intent = new Intent(MyselfActivity.this, ExplainActivity.class);
                    //用Bundle携带数据
                    Bundle bundle = new Bundle();
                    //传递name参数为tinyphp
                    bundle.putString("interlinkage", "file:////android_asset/app_updatainfo.html");

                    intent.putExtras(bundle);
                    startActivity(intent);

                } catch (Exception e) {
                    //异常处理
                    Tv_update.setText("打开失败！点击重试~");
                }

//                Intent intent = new Intent(MyselfActivity.this, TextRoundActivity.class);
//                startActivity(intent);


            }
        });


    }


    public static void openUserInfoByUid(Activity activity, String uid) {
        if (activity == null) {
            return;
        }
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory("android.intent.category.DEFAULT");
        intent.setData(Uri.parse("sinaweibo://userinfo?uid=" + uid));
        activity.startActivity(intent);
    }

    public static String packageName(Context context) {
        PackageManager manager = context.getPackageManager();
        String name = null;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            name = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return name;
    }

    public void initsqlite() {


        //adb forward tcp:8088 tcp:8088
        //5.实例化SQLiteOpenHelper的子类对象-MyDataBaseHelper
        helper = new DatabaseHelper(MyselfActivity.this, "dbdate", null, R.string.db_version);//dbName数据库名
        db = helper.getWritableDatabase();//获取到了 SQLiteDatabase 对象
    }


    private void Dialogaboutsoft(final String version) {


        new TDialog.Builder(getSupportFragmentManager())
                .setLayoutRes(R.layout.tdialog_about_soft)
                .setScreenWidthAspect(this, 1f)
                .setCancelableOutside(false)
                .setOnBindViewListener(new OnBindViewListener() {   //通过BindViewHolder拿到控件对象,进行修改
                    @Override
                    public void bindView(BindViewHolder bindViewHolder) {
                        bindViewHolder.setText(R.id.tv_version, version);
                    }
                })
                .addOnClickListener(R.id.iv_close)
                .setOnViewClickListener(new OnViewClickListener() {
                    @Override
                    public void onViewClick(BindViewHolder viewHolder, View view, TDialog tDialog) {
                        tDialog.dismiss();
                    }
                })
                .create()
                .show();
    }


    private void LoadToolbar() {
        //新页面接收数据


        Toolbar = (Toolbar) findViewById(R.id.toolbar);

        Toolbar.setTitle("个人中心");

        setSupportActionBar(Toolbar);//利用Toolbar代替ActionBar
        Toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到用户设置页面
                finish();
            }
        });

        Toolbar.inflateMenu(R.menu.menu_myself);
        //添加menu 菜单点击事件
        Toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_clear:

                        Intent intent = new Intent(MyselfActivity.this, DataClearActivity.class);
                        startActivity(intent);
                        break;

                }
                return true;
            }
        });

    }

    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_myself, menu);//toolbar添加menu菜单
        return true;
    }


}
